package org.wltea.analyzer.dic;

import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.plugin.analysis.ik.AnalysisIkPlugin;
import org.wltea.analyzer.help.ESPluginLoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Path;
import java.util.Properties;

/**
 * @author cloud
 * @description: 熱加載配置文件信息为单例信息
 * @date 2019/5/3 13:18
 **/
public final class HotReLoadDicConfig {
    private static final Logger logger = ESPluginLoggerFactory.getLogger(HotReLoadDicConfig.class.getName());
    //數據庫熱加載配置文件名
    public static final String SQL_HOT_PROPS_FILE = "jdbc-hot-reload.properties";
    private String jdbcDriver = "com.mysql.jdbc.Driver";
    private String jdbcUrl;
    private String jdbcUser;
    private String jdbcPassword;
    private String reloadSql;
    private String reloadStopWordSql;
    private Long reloadInterval;
    //是否有热更新数据的能力
    private boolean loadAble = true;
    //上一次查询得到的数据
    private Long reloadSqlCount = 0l;
    private Long reloadStopWordSqlCount = 0l;

    public String getJdbcUrl() {
        return jdbcUrl;
    }

    public HotReLoadDicConfig setJdbcUrl(String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
        return this;
    }

    public String getJdbcUser() {
        return jdbcUser;
    }

    public HotReLoadDicConfig setJdbcUser(String jdbcUser) {
        this.jdbcUser = jdbcUser;
        return this;
    }

    public String getJdbcPassword() {
        return jdbcPassword;
    }

    public HotReLoadDicConfig setJdbcPassword(String jdbcPassword) {
        this.jdbcPassword = jdbcPassword;
        return this;
    }

    public String getReloadSql() {
        return reloadSql;
    }

    public HotReLoadDicConfig setReloadSql(String reloadSql) {
        this.reloadSql = reloadSql;
        return this;
    }

    public String getReloadStopWordSql() {
        return reloadStopWordSql;
    }

    public HotReLoadDicConfig setReloadStopWordSql(String reloadStopWordSql) {
        this.reloadStopWordSql = reloadStopWordSql;
        return this;
    }

    public Long getReloadInterval() {
        return reloadInterval;
    }

    public HotReLoadDicConfig setReloadInterval(Long reloadInterval) {
        this.reloadInterval = reloadInterval;
        return this;
    }

    public String getJdbcDriver() {
        return jdbcDriver;
    }

    public HotReLoadDicConfig setJdbcDriver(String jdbcDriver) {
        this.jdbcDriver = jdbcDriver;
        return this;
    }

    public boolean isLoadAble() {
        return loadAble;
    }

    public void setLoadAble(boolean loadAble) {
        this.loadAble = loadAble;
    }

    public Long getReloadSqlCount() {
        return reloadSqlCount;
    }

    public HotReLoadDicConfig setReloadSqlCount(Long reloadSqlCount) {
        this.reloadSqlCount = reloadSqlCount;
        return this;
    }

    public Long getReloadStopWordSqlCount() {
        return reloadStopWordSqlCount;
    }

    public HotReLoadDicConfig setReloadStopWordSqlCount(Long reloadStopWordSqlCount) {
        this.reloadStopWordSqlCount = reloadStopWordSqlCount;
        return this;
    }

    public static HotReLoadDicConfig getInstance() {
        return HotReLoadDicConfigSign.CONFIG;
    }

    private static class HotReLoadDicConfigSign {
        public static final HotReLoadDicConfig CONFIG = new HotReLoadDicConfig();
    }

    public HotReLoadDicConfig() {
        //初始化數據庫熱加載配置文件
        Properties sqlHotProps = null;
        FileInputStream stream = null;
        try {
            sqlHotProps = new Properties();
            Path path = PathUtils
                    .get(new File(AnalysisIkPlugin.class.getProtectionDomain().getCodeSource().getLocation().getPath())
                            .getParent(), "config")
                    .toAbsolutePath();
            logger.info("文件加载路径:" + path.toString());
            File hotFile = path.resolve(HotReLoadDicConfig.SQL_HOT_PROPS_FILE).toFile();
            if (!hotFile.isFile()) {
                this.setLoadAble(false);
                return;
            }
            stream = new FileInputStream(hotFile);
            sqlHotProps.load(stream);
            String driver = sqlHotProps.getProperty("jdbc.driver", "");
            if (driver.length() <= 5) {
                this.setLoadAble(false);
                return;
            }
            this.setJdbcDriver(driver)
                    .setJdbcUrl(sqlHotProps.getProperty("jdbc.url", "").trim())
                    .setJdbcUser(sqlHotProps.getProperty("jdbc.user", "root").trim())
                    .setJdbcPassword(sqlHotProps.getProperty("jdbc.password", "").trim())
                    .setReloadSql(sqlHotProps.getProperty("jdbc.reload.sql", "").trim())
                    .setReloadStopWordSql(sqlHotProps.getProperty("jdbc.reload.stop.word.sql", "").trim())
                    .setReloadInterval(Long.parseLong(sqlHotProps.getProperty("jdbc.reload.interval", "5000")))
            ;
        } catch (Exception e) {
            logger.error("ik-analyzer 初始化數據庫熱加載配置文件异常", e);
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (Exception e) {
                    logger.error("ik-analyzer 初始化數據庫熱关闭流异常", e);
                }
            }
            sqlHotProps = null;
            logger.info("热加载参数完成:" + this.toString());
        }
    }

    @Override
    public String toString() {
        return "HotReLoadDicConfig{" +
                "jdbcDriver='" + jdbcDriver + '\'' +
                ", jdbcUrl='" + jdbcUrl + '\'' +
                ", jdbcUser='" + jdbcUser + '\'' +
                ", jdbcPassword='" + jdbcPassword + '\'' +
                ", reloadSql='" + reloadSql + '\'' +
                ", reloadStopWordSql='" + reloadStopWordSql + '\'' +
                ", reloadInterval=" + reloadInterval +
                ", loadAble=" + loadAble +
                ", reloadSqlCount=" + reloadSqlCount +
                ", reloadStopWordSqlCount=" + reloadStopWordSqlCount +
                '}';
    }
}
